package Dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;

import Bean.BeanOrder;
import Bean.BeanUser;


public class UserDao {
	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	public void useradd(String userid, String pwd,String username) throws Exception {
		String sql="select user_id from user where user_id=?";
		BeanUser user=this.jdbcTemplate.query(sql, new ResultSetExtractor<BeanUser>() {
			@Override
			public BeanUser extractData(ResultSet rs) throws SQLException, DataAccessException {
				// TODO Auto-generated method stub
				if(rs.next()) {
					BeanUser user=new BeanUser();
					user.setUser_id(rs.getString(1));
					return user;
				}
				else
					return null;
			}},userid);
		if(user==null) {
			sql="insert into user(user_id,pwd,user_name,user_type,user_status) values(?,?,?,1,1)";
			this.jdbcTemplate.update(sql,new Object[] {userid,pwd,username});
		}
		else throw new Exception("�û�id�ѱ�ռ��");
	}
	public void userupdate(String userid, String pwd,String username) throws Exception {
		String sql="update user set pwd=?,user_name=? where user_id=?";
		this.jdbcTemplate.update(sql,new Object[] {pwd,username,userid});
	}
	public void userdelete(String userid) throws Exception {
		String sql="update user set user_status=0 where user_id=?";
		this.jdbcTemplate.update(sql,new Object[] {userid});
	}
	public ArrayList<BeanUser> getUsersByKeyword(String keyword) throws Exception {
		String sql="select user_id,user_name,pwd from user where user_type=1 and user_status=1 and(user_id like '%"+keyword+"%' or user_name like '%"+keyword+"%')";
		ArrayList<BeanUser> users=new ArrayList<>();
		this.jdbcTemplate.query(sql, new ResultSetExtractor<BeanUser>() {
			@Override
			public BeanUser extractData(ResultSet rs) throws SQLException, DataAccessException {
				// TODO Auto-generated method stub
				while(rs.next()) {
					BeanUser user=new BeanUser();
					user.setUser_id(rs.getString(1));
					user.setUser_name(rs.getString(2));
					user.setPwd(rs.getString(3));
					user.setUser_status(1);
					users.add(user);
				}
				return null;
			}});
		return users;
	}
	public ArrayList<BeanUser> getUsers() throws Exception {
		String sql="select user_id,user_name,pwd from user where user_type=1 and user_status=1";
		ArrayList<BeanUser> users=new ArrayList<>();
		this.jdbcTemplate.query(sql, new ResultSetExtractor<BeanUser>() {
			@Override
			public BeanUser extractData(ResultSet rs) throws SQLException, DataAccessException {
				// TODO Auto-generated method stub
				while(rs.next()) {
					BeanUser user=new BeanUser();
					user.setUser_id(rs.getString(1));
					user.setUser_name(rs.getString(2));
					user.setPwd(rs.getString(3));
					user.setUser_status(1);
					users.add(user);
				}
				return null;
			}});
		return users;
	}
	public String checkuser(String userid, String pwd,int user_type) throws Exception {
		if (userid == null && userid=="")
			throw new Exception("�������û���");
		if (pwd == null)
			pwd = "";
		System.out.println(userid+" "+pwd);
		String selectStatement = "select user_name,pwd,user_type from user where user_id=?"; // sql����Լ�����
		 BeanUser user=this.jdbcTemplate.query(selectStatement, new ResultSetExtractor<BeanUser>() {
			@Override
			public BeanUser extractData(ResultSet rs) throws SQLException, DataAccessException {
				// TODO Auto-generated method stub
				BeanUser user=new BeanUser();
				if (rs.next()) {
					user.setPwd(rs.getString("pwd").replaceAll(" ", ""));
					user.setUser_name(rs.getString("user_name"));
					user.setUser_type(rs.getInt("user_type"));
					return user;
				}
				return null;
			}	
		}, userid);
		
		if(user==null) {
			throw new Exception("�û�������!");
		}
		if (!user.getPwd().equals(pwd)) {
			throw new Exception("�������!");
		}
		if (!(user.getUser_type()==user_type)) {
			throw new Exception("���ѡ�����!");
		}
		return user.getUser_name();
	}
}
